package com.pro01.backend.controller;

import com.pro01.backend.pojo.PageResult;
import com.pro01.backend.pojo.Result;
import com.pro01.backend.service.LotteryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;
import com.pro01.backend.utils.TokenUtil;

import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/admin/lottery")
public class AdminLotteryController {

    @Autowired
    private LotteryService lotteryService;

    // 管理员查看抽奖记录
    @GetMapping("/logs")
    public Result<PageResult<Map<String, Object>>> getAllLogs(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            HttpServletRequest request) {

        String role = TokenUtil.getRole(request);
        if (!"ADMIN".equals(role)) {
            return Result.error("无权限，只有管理员可以查看抽奖记录");
        }

        try {
            PageResult<Map<String, Object>> pageResult = lotteryService.getAllLotteryLogs(pageNum, pageSize);
            return Result.success(pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("查询失败：" + e.getMessage());
        }
    }

    // 管理员查看抽奖结果统计
    @GetMapping("/stats")
    public Result<List<Map<String, Object>>> getLotteryStats(HttpServletRequest request) {
        String role = TokenUtil.getRole(request);
        if (!"ADMIN".equals(role)) {
            return Result.error("无权限，只有管理员可以查看统计");
        }

        try {
            List<Map<String, Object>> result = lotteryService.getLotteryStats();
            return Result.success(result);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("统计失败：" + e.getMessage());
        }
    }

}
